window._GIS_MAP_DEBUG = true;

const uri = "./example/", version = "1.0.0";
var _gear, viewer;

import {msgType} from "./utils/msgType.js";

const init = e => {
  $G.create3D(
      {
        id: "mapBox",
        showGroundAtmosphere: true,
        debug: false,
        success: function (_viewer) {

          viewer = _viewer;
          _gear = new Gear(_viewer);

          $G.flyTo(viewer,{x:106.25958747885815,y:38.45011013540123,z:1124.75,h:0.82,p:-0.5240156676,r:6.28,duration:3});

          //监听摄像机视角高度
          _gear.DiyCameraMoveEndListener();

          if (e) {
            e();
          }
        },
      },
      Cesium
  );
};

//加载高德地图
function initMap_GaoDe(floor) {
  init(function () {
    //三维向vue返回消息
    $G.I.mouse.click.monitor3dMessage({window:window,viewer:viewer});
    //打印输出视角信息
    $G.I.mouse.click.printView({window:window,viewer:viewer});
    
    _gear.addBaseLayer_GaoDe();
  });
}

//加载在线地图
function initMap_online(floor) {
  init(function () {
    //三维向vue返回消息
    $G.I.mouse.click.monitor3dMessage({window:window,viewer:viewer});
    //打印输出视角信息
    $G.I.mouse.click.printView({window:window,viewer:viewer});

    // if(window.Intranet){   //银川内网调用超图天地图底图
    //   //添加wmts自定义底图
    //   _gear.addWmtsMap(viewer);
    // }else{//外网调用在线天地图底图
      //在线天地图底图
      _gear.addBaseLayer();
    // }
    // //相机转跳定位模型
    // _gear.flyToTiltPhotography("0");
  });
}

//加载离线超图地图
function initMap_offline_SuperMap(params) {
  init(function () {
    //三维向vue返回消息
    $G.I.mouse.click.monitor3dMessage({window:window,viewer:viewer});
    //打印输出视角信息
    $G.I.mouse.click.printView({window:window,viewer:viewer});

    // if(window.Intranet){   //银川内网调用超图天地图底图
      //添加wmts自定义底图
      _gear.addWmtsMap(viewer,params);
    // }else{//外网调用在线天地图底图
    //   //天地图底图
    //   _gear.addBaseLayer();
    // }

    //获取视角]
    // _gear.getCameraView();
    // //相机转跳定位模型
    // _gear.flyToTiltPhotography("0");

  });
}

function initMap_offline_CustomMap(){
  init(function () {
    //三维向vue返回消息
    $G.I.mouse.click.monitor3dMessage({window:window,viewer:viewer});
    //打印输出视角信息
    $G.I.mouse.click.printView({window:window,viewer:viewer});

    //加载离线底图
    var gaode1 = viewer.imageryLayers.addImageryProvider(new Cesium.UrlTemplateImageryProvider({
      url : "http://121.40.42.254:8008/兴庆区地图底图/{z}/{x}/{y}.png",
      fileExtension : "png",
    }));

    //获取视角]
    _gear.getCameraView();
    // //相机转跳定位模型
    // _gear.flyToTiltPhotography("0");
  });
}

function addCamera(data) {  
  //id、name、gis信息、类型、icon、url
  _gear.add_camera(data);
}

//接收消息
window.addEventListener("message", function (event) {
  // console.log('html中获取vue中的数据----：', event.data);
  if (!event.data) {
    return;
  }

  if (event.data.type === msgType.initMap_GaoDe) {//加载高德卫星地图
    initMap_GaoDe();
  }else if (event.data.type === msgType.initMap_online) {//加载在线地图
    initMap_online();
  }else if (event.data.type === msgType.initMap_offline_SuperMap) {//加载离线超图地图
    initMap_offline_SuperMap(event.data.params);
  }else if (event.data.type === msgType.initMap_offline_CustomMap) {//加载离线地图
    initMap_offline_CustomMap();
  }else if (event.data.type === msgType.getPerspective) {//获取视角
    _gear.getPerspective(event.data.params);
  }else if (event.data.type === msgType.flyTo) {//飞行到指定位置
    _gear.flyTo(event.data.params);
  }else if (event.data.type === msgType.addTiltPhotography) {//加载倾斜摄影
    _gear.addTiltPhotography(event.data.params.id,event.data.params.name);
  }else if (event.data.type === msgType.destroyTiltPhotography) {//销毁倾斜摄影
    _gear.destroy_TiltPhotography(event.data.params);
  }else if (event.data.type === msgType.addGridData) {//添加网格数据
    _gear.example_geoJson_load();
  }else if (event.data.type === msgType.addGridData_1) {//添加网格数据_业主提供
    _gear.example_geoJson_load_1(event.data.params);
  }else if (event.data.type === msgType.addGridData_2) {//添加网格数据_业主提供
    _gear.example_geoJson_load_2(event.data.params);
  }else if (event.data.type === msgType.destroyGridData) {//销毁网格数据
    _gear.destroyGridData(event.data.params);
  }else if (event.data.type === msgType.addCamera) {//添加摄像头
    addCamera(event.data.params);
  }else if (event.data.type === msgType.multiStreamCamera) {//多流摄像头
    _gear.multiStreamCamera(event.data.params);
  }else if (event.data.type === msgType.destroyCamera) {//销毁摄像头
    _gear.destroy_camera(event.data.params);
  }else if (event.data.type === msgType.destroyMultiStreamCamera) {//销毁多流摄像头
    _gear.destroyMultiStreamCamera(event.data.params);
  }else if (event.data.type === msgType.addBaiMo) {//加载白膜
    _gear.addBaiMo(event.data.params);
  }else if (event.data.type === msgType.baiMoEffects) {//白膜特效
    _gear.baiMoEffects(event.data.params);
  }else if (event.data.type === msgType.destroyBaiMo) {//销毁白膜
    _gear.destroyBaiMo(event.data.params);
  }else if (event.data.type === msgType.electronicFence) {//电子围栏
    _gear.electronicFence(event.data.params);
  }else if (event.data.type === msgType.electronicLabel) {//电子标签
    _gear.electronicLabel(event.data.params);
  }else if (event.data.type === msgType.poiLabel) {//POI标注
    _gear.poiLabel(event.data.params);
  }else if (event.data.type === msgType.destroyPoiLabel) {//销毁POI标注
    _gear.destroy_PoiLable(event.data.params);
  }else if (event.data.type === msgType.alarm) {//告警
    _gear.alarm(event.data.params);
  }else if (event.data.type === msgType.circleRipple) {//波纹
    _gear.circleRipple(event.data.params);
  }else if (event.data.type === msgType.raderScan) {//雷达扫描
    _gear.raderScan(event.data.params);
  }else if (event.data.type === msgType.rotationPoint) {//旋转点位
    _gear.rotationPoint(event.data.params);
  }else if (event.data.type === msgType.roadLine) {//道路线
    _gear.roadLine(event.data.params);
  }else if (event.data.type === msgType.ascendingParticle) {//上升粒子
    _gear.ascendingParticle(event.data.params);
  }
});

//发送消息
function sendMsgToVue(data) {
  if (window != window.parent) {
    window.parent.postMessage(data, '*');
  }
}
